$(document).ready(function(){

	//退出--个人中心--主页
	$("#exitUser_tomain,#exitUser_tomain2").click(function(){
		$.ajax({
			url:"exitToUser",
			success:function(){
				location.href="/Home/User/main";
			}
		});
	});

	//退出--提交需求页面
	$("#exitUser_tosc").click(function(){
        window.location.href="exitToSC";
	});

    //提交需求--左-验证联系人
    $("#submitReqLeft_name").keyup(function(){
        $(this).blur(function(){
            hideErrorWithRemoveClass("submitReqLeft_name_error","&nbsp;","submitReqLeft_name");
            var name=$("#submitReqLeft_name").val();
            if(name==""){
                showErrorWithAddClass("submitReqLeft_name_error","请输入您的联系人","submitReqLeft_name");
                return false;
            }
            if(!name.match(/^([\u4e00-\u9fa5]{1,10}|[a-zA-Z]{1,20})$/)){
                showErrorWithAddClass("submitReqLeft_name_error","格式不正确","submitReqLeft_name");
                return false;
            }
        });
    });

    //提交需求--左-验证手机号
    $("#submitReqLeft_phone").keyup(function(){
        $(this).blur(function(){
            hideErrorWithRemoveClass("submitReqLeft_phone_error","&nbsp;","submitReqLeft_phone");
            var phone=$(this).val();
            if(phone==""){
                showErrorWithAddClass("submitReqLeft_phone_error","请输入中国大陆手机号","submitReqLeft_phone");
                return false;
            }
            if(!phone.match(/^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/)){
                showErrorWithAddClass("submitReqLeft_phone_error","请输入有效的手机号","submitReqLeft_phone");
                return false;
            }
        });
    });

    //提交需求--左-验证所处行业
    $("#submitReqLeft_industry").keyup(function(){
        $(this).blur(function(){
            hideErrorWithRemoveClass("submitReqLeft_industry_error","&nbsp;","submitReqLeft_industry");
            var industry=$(this).val();
            if(industry==""){
                showErrorWithAddClass("submitReqLeft_industry_error","请输入您所处的行业名称","submitReqLeft_industry");
                return false;
            }
            if(!industry.match(/^([\u4e00-\u9fa5a-zA-Z\s]{1,10})$/)){
                showErrorWithAddClass("submitReqLeft_industry_error","格式不正确","submitReqLeft_industry");
                return false;
            }
        });
    });

    //提交需求--左-验证咨询预算
    $("#submitReqLeft_budget").keyup(function(){
        $(this).blur(function(){
            hideErrorWithRemoveClass("submitReqLeft_budget_error","&nbsp;","submitReqLeft_budget");
            var industry=$(this).val();
            if(industry==""||!industry.match(/^[0-9]{1,18}$/)){
                showErrorWithAddClass("submitReqLeft_budget_error","请输入您的预算费用","submitReqLeft_budget");
                return false;
            }
        });
    });

	//提交需求--左
	$("#submitReqLeftBtn").click(function(){
		var name=$("#submitReqLeft_name").val();
		var phone=$("#submitReqLeft_phone").val();
		var industry=$("#submitReqLeft_industry").val();
		var budget=$("#submitReqLeft_budget").val();

        var name_error=$("#submitReqLeft_name_error").html();
        var phone_error=$("#submitReqLeft_phone_error").html();
        var industry_error=$("#submitReqLeft_industry_error").html();
        var budget_error=$("#submitReqLeft_budget_error").html();

        // $(".submitReqLeft-tishi").css("border-color","#e6e6e6");
        //验证
        if(name_error!="&nbsp;"||phone_error!="&nbsp;"||industry_error!="&nbsp;"||budget_error!="&nbsp;"){
            return false;
        }
        if(name==""){
            showErrorWithAddClass("submitReqLeft_name_error","请输入您的联系人","submitReqLeft_name");
            return false;
        }
        if(phone==""){
            showErrorWithAddClass("submitReqLeft_phone_error","请输入您的手机号","submitReqLeft_phone");
            return false;
        }
        if(industry==""){
            showErrorWithAddClass("submitReqLeft_industry_error","请输入您所处的行业","submitReqLeft_industry");
            return false;
        }
        if(budget==""){
            showErrorWithAddClass("submitReqLeft_budget_error","请输入您的项目预算","submitReqLeft_budget");
            return false;
        }
		$.ajax({
			url:"submitReqLeftClick",
			dataType:'json',
			data:{n:name,p:phone,i:industry,b:budget},
			type:'post',
			success:function(data){
				if(data.status==1){
                    $(".bg").hide();
                    $(".zixunSuccess-tishi").show();
                    return false;
				}else{
                    $(".bg").hide();
                    $(".zixunFailed-tishi").show();
                    return false;
				}
				
			}
		});
	});

	//项目类型--值
    var project_title=0;//全局
	$(".xiangmu-ul1 img").click(function(){
		project_title=$(this).attr("alt");
	});

	//提交需求--右--第一步--提交.attr("alt")
	$("#submitReqRightOne_next").click(function(){
        var project_service = "";
        $(".xiangmu-ul2 li").each(function(){
            var ul2_selected = $(this).hasClass("xiangmu-selected");
            if(ul2_selected == true) {
                project_service = $(this).children("img").attr("alt")+';'+project_service;
            }
        });
        //验证
        if(project_title == "" || project_service == "") {
            return false;
        }
        $.post("getProjectTitleAndService",{pt:project_title,ps:project_service},
            function(){location.href="submitReqRightTwo";});
	});

	//提交需求--右--第二步--验证
	//验证联系人
	$("#submitReqRight_name").keyup(function(){
        $(this).blur(function(){
            hideErrorWithRemoveClass("submitReqRight_name_error","&nbsp;","submitReqRight_name");
            var uname=$(this).val();
            if(uname==""){
                showErrorWithAddClass("submitReqRight_name_error","请输入您的联系人","submitReqRight_name");
                return false;
            }
            if(!uname.match(/^([\u4e00-\u9fa5]{1,10}|[a-zA-Z]{1,20})$/)){
                showErrorWithAddClass("submitReqRight_name_error","请输入正确的格式","submitReqRight_name");
                return false;
            }
        });
    });

    //验证手机号
    $("#submitReqRight_phone").keyup(function(){
        $(this).blur(function(){
            hideErrorWithRemoveClass("submitReqRight_phone_error","&nbsp;","submitReqRight_phone");
            var phone=$(this).val();
            if(phone==""){
                showErrorWithAddClass("submitReqRight_phone_error","请输入中国大陆手机号","submitReqRight_phone");
                return false;
            }
            if(!phone.match(/^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/)){
                showErrorWithAddClass("submitReqRight_phone_error","请输入有效的手机号","submitReqRight_phone");
                return false;
            }
        });
    });

    //验证项目名称
    $("#submitReqRight_pname").keyup(function(){
        $(this).blur(function(){
            hideErrorWithRemoveClass("submitReqRight_pname_error","&nbsp;","submitReqRight_pname");
            var pname=$(this).val();
            if(pname==""){
                showErrorWithAddClass("submitReqRight_pname_error","请输入您的项目名称","submitReqRight_pname");
                return false;
            }
        });
    });

    //验证项目预算
    $("#submitReqRight_pbudget").keyup(function(){
        $(this).blur(function(){
            hideErrorWithRemoveClass("submitReqRight_pbudget_error","&nbsp;","submitReqRight_pbudget");
            var budget=$(this).val();
            if(budget==""){
                showErrorWithAddClass("submitReqRight_pbudget_error","请输入您的项目预算","submitReqRight_pbudget");
                return false;
            }
            // if(!budget.match(/^([1-9]{1}+[0-9]{1,18})$/)){
            //     showErrorWithAddClass("submitReqRight_pbudget_error","请输入您的项目预算","submitReqRight_pbudget");
            //     return false;
            // }
        });
    });

    //验证项目简介
    $("#submitReqRight_presume").keyup(function(){
        $(this).blur(function(){
            hideErrorWithRemoveClass("submitReqRight_presume_error","&nbsp;","submitReqRight_presume");
        });
    });

	//提交需求--右--第二步--提交
	$("#submitReqRightTwo_next").click(function(){
        var data = new FormData($("#uploadForm")[0]);

        var name=$("#submitReqRight_name").val();
        var phone=$("#submitReqRight_phone").val();
        var pname=$("#submitReqRight_pname").val();
        var budget=$("#submitReqRight_pbudget").val();
        var resume=$("#submitReqRight_presume").val();
        var name_error=$("#submitReqRight_name_error").html();
        var phone_error=$("#submitReqRight_phone_error").html();
        var pname_error=$("#submitReqRight_pname_error").html();
        var budget_error=$("#submitReqRight_pbudget_error").html();
        var resume_error=$("#submitReqRight_presume_error").html();
        // var error=$(".submitReqRightTwo-tishi").html();

        if(name_error!="&nbsp;"||phone_error!="&nbsp;"||pname_error!="&nbsp;"||budget_error!="&nbsp;"||resume_error!="&nbsp;"){
            return false;
        }
        if(name==""){
            showErrorWithAddClass("submitReqRight_name_error","请输入您的联系人","submitReqRight_name");
            return false;
        }
        if(phone==""){
            showErrorWithAddClass("submitReqRight_phone_error","请输入您的手机号","submitReqRight_phone");
            return false;
        }
        if(pname==""){
            showErrorWithAddClass("submitReqRight_pname_error","请输入您的项目名称","submitReqRight_pname");
            return false;
        }
        if(budget==""){
            showErrorWithAddClass("submitReqRight_pbudget_error","请输入您的项目预算","submitReqRight_pbudget");
            return false;
        }
        if(resume==""){
            showErrorWithAddClass("submitReqRight_presume_error","请输入您的项目简介","submitReqRight_presume");
            return false;
        }
        $.ajax({
            url: "submitReqRightTwoClick",
            data: data,
            cache: false,
            contentType: false,
            processData: false,
            dataType: "json",
            type: 'POST',
            success: function(data){
            if(data.status==1){
                $(".bg").hide();
                $(".zixunSuccess-tishi").show();
                return false;
            }
            else if(data.status==0){
                $(".bg").hide();
                $(".zixunFailed-tishi").show();
                return false;
            }
            else if(data.status==2){
                $(".bg").hide();
                $("#zixunFailed-div").replaceWith("<div class='tishi-text1'>"+data.info+"</div>");
                $(".zixunFailed-tishi").show();
                return false;
            }else{
                $(".bg").hide();
                $("#zixunFailed-div").replaceWith('<div class="tishi-text1">文件存入数据库失败</div>');
                $(".zixunFailed-tishi").show();
                return false;
            }
         }
        });
	});

	/*personalCenter.html*/
	//验证邮箱
    $("#pc_email").keyup(function(){
        $(this).blur(function(){
            hideErrorWithRemoveClass("pc_email_error","&nbsp;","pc_email");
			
            var pcEmail=$("#pc_email").val();
            if(pcEmail==""){
                showErrorWithAddClass("pc_email_error","请输入您的邮箱","pc_email");
                return false;
            }
            if(!pcEmail.match(/^[0-9a-zA-Z_-]+@+[0-9a-zA-Z_-]+.+[0-9a-zA-Z]$/)){
                showErrorWithAddClass("pc_email_error","请输入正确的邮箱格式","pc_email");
                return false;
            }
            $.ajax({
                url:"emailUniqueExceptSelf",
                data:{mailNumber:pcEmail},
                dataType:'json',
                type:'post',
                success:function(data){
                    if(data.status==1){
                        showErrorWithAddClass("pc_email_error","邮箱已存在","pc_email");
                        return false;
                    }
                }
            });
        });
    });
	
	//验证姓名(中英文不能同时出现，长度)
    $("#pc_name").keyup(function(){
        $(this).blur(function(){
            hideErrorWithRemoveClass("pc_name_error","&nbsp;","pc_name");

            var uname=$(this).val();
            if(uname==""){
                showErrorWithAddClass("pc_name_error","请输入您的姓名","pc_name");
                return false;
            }
            // if(!uname.match(/^([\u4e00-\u9fa5]{0,20}|[a-zA-Z\.\s]{0,20})$/)){
            if(!uname.match(/^([\u4e00-\u9fa5]{0,20})$/)){
                showErrorWithAddClass("pc_name_error","请输入正确的姓名格式","pc_name");
                return false;
            }
        });
    });

    //验证手机号
    $("#pc_phone").keyup(function(){
        $(this).blur(function(){
            hideErrorWithRemoveClass("pc_phone_error","&nbsp;","pc_phone");

            var phone=$(this).val();
            if(phone==""){
                showErrorWithAddClass("pc_phone_error","请输入中国大陆手机号","pc_phone");
                return false;
            }
            if(!phone.match(/^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/)){
                showErrorWithAddClass("pc_phone_error","请输入有效的手机号","pc_phone");
                return false;
            }
            //手机号唯一
            $.ajax({
                url:"phoneUnique",
                data:{phoneNum:phone},
                dataType:'json',
                success:function(data){
                    if(data.status==1){
                        showErrorWithAddClass("pc_phone_error","手机号已存在","pc_phone");
                        return false;
                    }
                }
            });
        });
    });

	//验证微信号
    $("#pc_weixin").keyup(function(){
        $(this).blur(function(){
            var weixinNum=$(this).val();
            hideErrorWithRemoveClass("pc_weixin_error","&nbsp;","pc_weixin");
            if(weixinNum==""){
                showErrorWithAddClass("pc_weixin_error","请输入您的微信号","pc_weixin");
                return false;
            }
            // if(!weixinNum.match(/^[a-zA-Z][a-zA-Z0-9_\-\u4e00-\u9fa5]{5,20}$/)){
            if(!weixinNum.match(/^[a-zA-Z0-9_\-\u4e00-\u9fa5]{5,20}$/)){
                showErrorWithAddClass("pc_weixin_error","微信格式不正确","pc_weixin");
                return false;
            }
        });
    });

    //验证qq
    $("#pc_qq").keyup(function(){
        $(this).blur(function(){
            var qq=$(this).val();
            hideErrorWithRemoveClass("pc_qq_error","&nbsp;","pc_qq");
            if(qq==""){
                showErrorWithAddClass("pc_qq_error","请输入您的qq号","pc_qq");
                return false;
            }
            if(!qq.match(/^[0-9]{5,12}$/)){
                showErrorWithAddClass("pc_qq_error","qq格式不正确","pc_qq");
                return false;
            }
        });
    });

	//点击保存
	$("#pc_save").click(function(){
		var email=$("#pc_email").val();
		var name=$("#pc_name").val();
		var phone=$("#pc_phone").val();
		var weixin=$("#pc_weixin").val();
		var qq=$("#pc_qq").val();
        var error = $(".pc_error").html();
		var email_error=$("#pc_email_error").html();
        var name_error=$("#pc_name_error").html();
        var phone_error=$("#pc_phone_error").html();
        var wx_error=$("#pc_weixin_error").html();
        var qq_error=$("#pc_qq_error").html();

		if(email_error != "&nbsp;" || name_error != "&nbsp;" || phone_error != "&nbsp;" || wx_error != "&nbsp;" || qq_error != "&nbsp;"){
            return false;
        }
        // if(error != "&nbsp;"){
        //     return false;
        // }
        hideErrorWithRemoveClassByClass("pc_error","&nbsp;","pc_input");
        if(email==""){
            showErrorWithAddClass("pc_email_error","请输入邮箱号","pc_email");
			return false;
		}
        if(name==""){
            showErrorWithAddClass("pc_name_error","请输入姓名","pc_name");
            return false;
        }
        if(phone==""){
            showErrorWithAddClass("pc_phone_error","请输入手机号","pc_phone");
            return false;
        }
        if(weixin==""){
            showErrorWithAddClass("pc_weixin_error","请输入微信","pc_weixin");
            return false;
        }
        if(qq==""){
            showErrorWithAddClass("pc_qq_error","请输入qq号","pc_qq");
            return false;
        }
        $.ajax({
            url:"pcSave",
            data:{e:email,n:name,p:phone,wx:weixin,q:qq},
            dataType:'json',
            type:'post',
            success:function(data){
                if(data.status==1){//注册成功
                    hideErrorWithRemoveClassByClass("pc_error","&nbsp;","pc_input");
                    $(".bg").hide();
                    $(".saveSuccess-tishi").show();
                }else{
                    $(".bg").hide();
                    $(".saveFailed-tishi").show();
                }
            }
        });
	});

});

/*显示错误信息*/
function showErrorWithAddClass(id1,html1,id2){
    $("#"+id1).show();
    $("#"+id1).html(html1);
    $("#"+id2).css("border-color","#ff0000");
}

/*隐藏错误信息*/
function hideErrorWithRemoveClass(id1,html1,id2){
    $("#"+id1).hide();
    $("#"+id1).html(html1);
    $("#"+id2).css("border-color","#e6e6e6");
}

function hideErrorWithRemoveClassByClass(class1,html1,class2){
    $("."+class1).hide();
    $("."+class1).html(html1);
    $("."+class2).css("border-color","#e6e6e6");
}